package com.thrd.taiexhibition;

import android.content.Context;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.OutputStream;

public class ShellCmd {
    private static final String TAG="ShellCmd";
    private static ShellCmd instance;
    private Context context;
    private Thread thread;

    private ShellCmd(Context context) {
        this.context = context;
    }

//    private ShellCmd() {
//    }

    public static ShellCmd getInstance(Context context) {
        if (instance == null) {
            synchronized (ShellCmd.class) {
                if (instance == null) {
                    instance = new ShellCmd(context);
                }
            }
        }
        return instance;
    }




    private void execShellCmd(String cmd) {
        try {
            // 申请获取root权限，这一步很重要，不然会没有作用
            Process process = Runtime.getRuntime().exec("su");
            // 获取输出流
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        } catch ( Throwable e) {
//            t.printStackTrace()
        }
    }

    /**
     * 点击事件
     * @param x 绝对坐标
     * @param y 相对坐标
     */
    private void lickOn( int x,int y){
        execShellCmd("input tap "+x+" "+y);
    }



    /**
     * 长按事件
     * @param x 绝对坐标
     * @param y 相对坐标
     * @param time 事件毫秒
     */
    private void pressOn(int x,int y,long time){
        execShellCmd("input swipe "+x+" "+y+" "+time);
    }

    /**
     * 滑动
     * @param xDown 按下X绝对坐标
     * @param yDown 按下Y相对坐标
     * @param xBounce 弹起X绝对坐标
     * @param yBounce 弹起Y相对坐标
     */
    private void slideOn(int xDown,int yDown,int xBounce,int yBounce){
        execShellCmd("input swipe "+xDown+" "+yDown+" "+xBounce+" "+yBounce);
    }




    public void skateboardHangUp() {
        if(thread==null||!thread.isAlive()){
            isStop=false;
            thread = new Thread(runnable);
            thread.start();
       //     Toast.makeText(context,"开启滑板挂机模式",0).show();
        }else{
            isStop=true;
          //  thread.stop();
       //     Toast.makeText(context,"停止滑板挂机模式",0).show();
        }

    }

    private boolean isStop=false;

   private Runnable runnable=new Runnable() {
        @Override
        public void run() {
            try {
                while (!isStop) {
                    Thread.sleep(1000);
                    //开始匹配
                    Log.e(TAG,"开始匹配");
                    lickOn(845, 650);
                    Thread.sleep(1000);
                    //结算
                    lickOn(654, 639);
                    Log.e(TAG,"结算");
                    Thread.sleep(1000);
                    //返回
                    lickOn(165, 661);
                    Log.e(TAG,"返回");
                    Thread.sleep(1000);
                }
            }catch (Exception e){

            }

        }
    };





    private OutputStream os;

    /**
     * 执行shell指令
     *
     * @param cmd
     *            指令
     */
    public final void exec(String cmd) {
        try {
            if (os == null) {
                os = Runtime.getRuntime().exec("su").getOutputStream();
            }
            DataOutputStream dataOutputStream = new DataOutputStream(os);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
//            os.write(cmd.getBytes());
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 后台模拟全局按键
     *
     * @param keyCode
     *            键值
     */
    public final void simulateKey(int keyCode) {
        execShellCmd("input keyevent " + keyCode + "\n");
    }

}
